package labilabyrinth.game;

import android.content.Context;
import android.os.SystemClock;
import android.widget.Chronometer;
 
import java.text.DecimalFormat;
import java.text.NumberFormat;
 
public class Timer extends Chronometer {
 
  private long eclipsedTime;
  public String currentTime;  
  
  public Timer(Context context) {
    super(context);
    init();
  }
 
  public void start() {
    setBase(SystemClock.elapsedRealtime() - eclipsedTime);
    super.start();
  }
 
  public void restart() {
    stop();
    this.eclipsedTime = 0l;
    start();
  }
 
  public void stop() {
    this.eclipsedTime = SystemClock.elapsedRealtime() - this.getBase();
    super.stop();
  }
 
  public long stopAndReturnEclipsedTime() {
    stop();
    return this.eclipsedTime;
  }
 
  private void init() {
    this.eclipsedTime = 0l;
    this.setOnChronometerTickListener(new OnChronometerTickListener() {
      NumberFormat formatter = new DecimalFormat("00");
 
      public void onChronometerTick(Chronometer arg0) {
        float countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
        String asText = formatter.format(countUp / 60) + ":" + formatter.format(countUp % 60);
        currentTime = asText;
      }
    });
  }
}
